package com.fangcun.service.api.impl;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fangcun.bean.api.user.request.H5BindPhoneRequest;
import com.fangcun.bean.api.user.request.H5WechatLoginRequest;
import com.fangcun.bean.api.user.request.PhoneLoginRequest;
import com.fangcun.bean.api.user.request.SendVerificationCodeRequest;
import com.fangcun.bean.api.user.request.UserUpdateRequest;
import com.fangcun.bean.api.user.request.XcxLoginRequest;
import com.fangcun.bean.api.user.response.UserResponse;
import com.fangcun.common.auth.UserAuthMethod;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.canstant.PublicConstant;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProRunTimeException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.common.util.BeanMapper;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.base.BaseService;
import com.fangcun.common.util.date.DateUtils;
import com.fangcun.common.util.json.JSONTools;
import com.fangcun.common.util.qiniu.QiniuUtils;
import com.fangcun.common.util.sms.SendSms;
import com.fangcun.common.util.tencent.TencentLoginBean;
import com.fangcun.common.util.tencent.TencentUtils;
import com.fangcun.common.util.tencent.WechatAccessToken;
import com.fangcun.common.util.tencent.WechatUserInfo;
import com.fangcun.entity.SysSendVerificationCodeData;
import com.fangcun.entity.User;
import com.fangcun.entity.UserLogin;
import com.fangcun.mapper.SysSendVerificationCodeDataMapper;
import com.fangcun.mapper.UserLoginMapper;
import com.fangcun.mapper.UserMapper;
import com.fangcun.service.api.ApiUserService;

/**
 * @作者： 陳曉鬆 @时间： 2020年4月27日 - 上午9:27:42 @description：->
 */
@Service("apiUserService")
public class ApiUserServiceImpl extends BaseService implements ApiUserService {

	@Autowired
	protected UserMapper userMapper;
	@Autowired
	protected UserLoginMapper userLoginMapper;
	@Autowired
	protected SysSendVerificationCodeDataMapper sysSendVerificationCodeDataMapper;
	@Autowired
	protected TencentUtils tencentUtils;
	@Autowired
	protected QiniuUtils qiniuUtils;

	@Override
	@Transactional
	public UserResponse xcxLogin(XcxLoginRequest requestParam) throws ProAffairException {
		if (StringUtils.isEmpty(requestParam.getAppid()) || StringUtils.isEmpty(requestParam.getCode())) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		TencentLoginBean tencentLoginBean = tencentUtils.getOpenId(requestParam.getAppid(), requestParam.getCode());
		if (tencentLoginBean == null || StringUtils.isEmpty(tencentLoginBean.getOpenid())) {
			throw new ProRunTimeException(ErrCode.USER_GETWECHAT_FAIL.getReturnCode(), ErrCode.USER_GETWECHAT_FAIL.getReturnMsg());
		}
		User user = userMapper.selectByAppidAndOpenid(requestParam.getAppid(), tencentLoginBean.getOpenid());
		if (user == null) {
			user = new User();
			user.setUserName("数营_" + tencentLoginBean.getOpenid().substring(tencentLoginBean.getOpenid().length() - 6));
			if (requestParam.getRecommendUserId() != null && PublicMethod.isNumber(requestParam.getRecommendUserId().toString())) {
				user.setRecommendUserId(requestParam.getRecommendUserId());
			}
			if (!StringUtils.isEmpty(tencentLoginBean.getUnionid())) {
				user.setUnionid(tencentLoginBean.getUnionid());
			}
			int result = userMapper.insertSelective(user);
			if (result == 0 || user.getId() == null) {
				throw new ProRunTimeException(ErrCode.USER_DB_INSERT_FAIL.getReturnCode(), ErrCode.USER_DB_INSERT_FAIL.getReturnMsg());
			}
			UserLogin login = new UserLogin();
			login.setUserId(user.getId());
			login.setAppid(requestParam.getAppid());
			login.setOpenid(tencentLoginBean.getOpenid());
			if (userLoginMapper.insertSelective(login) == 0) {
				throw new ProRunTimeException(ErrCode.USER_DB_INSERT_FAIL.getReturnCode(), ErrCode.USER_DB_INSERT_FAIL.getReturnMsg());
			}
			user = userMapper.selectByAppidAndOpenid(requestParam.getAppid(), tencentLoginBean.getOpenid());
		} else {// 更改最后登录时间
			user.setLastLoginTime(new Date());
		}
		if (user == null) {
			throw new ProRunTimeException(ErrCode.USER_LOGIN_FAIL.getReturnCode(), ErrCode.USER_LOGIN_FAIL.getReturnMsg() + ":无该用户信息");
		}
		setLoginToken(user);
		return BeanMapper.map(user, UserResponse.class);
	}

	@Override
	@Transactional
	public UserResponse phoneLogin(PhoneLoginRequest requestParam) throws ProAffairException {
		if (StringUtils.isEmpty(requestParam.getPhone())) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), "请传入正确的手机号");
		}
		if (PhoneLoginRequest.TYPE_1 == requestParam.getType() && StringUtils.isEmpty(requestParam.getPassword())) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), "请传入正确的密码");
		}
		if (PhoneLoginRequest.TYPE_2 == requestParam.getType() && StringUtils.isEmpty(requestParam.getVerificationCode())) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), "请传入正确的验证码");
		}
		User user = userMapper.selectByPhone(requestParam.getPhone());
		if (PhoneLoginRequest.TYPE_1 == requestParam.getType()) {// 手机号密码登录
			if (user == null) {
				throw new ProRunTimeException(ErrCode.USER_PHONE_NO_EXIST.getReturnCode(), ErrCode.USER_PHONE_NO_EXIST.getReturnMsg());
			}
			UserLogin login = userLoginMapper.selectByUserIdForPhoneLogin(user.getId());
			if (login == null) {
				throw new ProRunTimeException(ErrCode.USER_PHONE_NO_SET_PASSWORD.getReturnCode(), ErrCode.USER_PHONE_NO_SET_PASSWORD.getReturnMsg());
			}
			if (!PublicMethod.getMd5(requestParam.getPassword(), 2, true).equals(login.getPassword())) {
				throw new ProRunTimeException(ErrCode.USER_LOGIN_PASSWORD_FAIL.getReturnCode(), ErrCode.USER_LOGIN_PASSWORD_FAIL.getReturnMsg());
			}
		}
		if (PhoneLoginRequest.TYPE_2 == requestParam.getType()) {// 手机号验证码登录
			// 验证 验证码
			String redisKey = requestParam.getPhone() + "_" + PublicConstant.VERIFICATION_CODE_TYPE_4;
			Object obj = redisService.get(UserAuthMethod.LOGIN_REDIS_DB, redisKey);
			if (StringUtils.isEmpty(obj)) {
				throw new ProRunTimeException(ErrCode.VERIFICATIONCODE_BE_OVERDUE.getReturnCode(), ErrCode.VERIFICATIONCODE_BE_OVERDUE.getReturnMsg());
			}
			if (!obj.toString().equals(requestParam.getVerificationCode())) {
				throw new ProRunTimeException(ErrCode.VERIFICATIONCODE_FAIL.getReturnCode(), ErrCode.VERIFICATIONCODE_FAIL.getReturnMsg());
			}
			redisService.remove(UserAuthMethod.LOGIN_REDIS_DB, redisKey);

			if (user == null) {// 注册这个手机号
				user = new User();
				user.setPhone(requestParam.getPhone());
				user.setUserName("用户_" + requestParam.getPhone().substring(requestParam.getPhone().length() - 4));
				if (requestParam.getRecommendUserId() != null && PublicMethod.isNumber(requestParam.getRecommendUserId().toString())) {
					user.setRecommendUserId(requestParam.getRecommendUserId());
				}
				if (userMapper.insertSelective(user) == 0) {
					throw new ProRunTimeException(ErrCode.USER_DB_INSERT_FAIL.getReturnCode(), ErrCode.USER_DB_INSERT_FAIL.getReturnMsg());
				}
				user = userMapper.selectByPhone(requestParam.getPhone());
			}
		}
		setLoginToken(user);
		UserResponse response = BeanMapper.map(user, UserResponse.class);
		if (!StringUtils.isEmpty(requestParam.getAppId())) {
			if (userLoginMapper.selectByUserIdAndAppid(user.getId(), requestParam.getAppId()) != null) {
				response.setIsBindWeChat(2);
			} else {
				response.setIsBindWeChat(1);
			}
		}
		return response;
	}

	/** 设置返回token 存入redis **/
	public void setLoginToken(User user) throws ProAffairException {
		user.setLastLoginTime(new Date());
		userMapper.updateByPrimaryKeySelective(user);
		String key = UserAuthMethod.getLoginRedisKey(user.getPhone(), user.getId());
		String token = UserAuthMethod.getLoginToken(key, user.getLastLoginTime().getTime());
		if (ErrCode.ERR.getReturnMsg().equals(token)) {
			throw new ProRunTimeException(ErrCode.USER_LOGIN_FAIL.getReturnCode(), ErrCode.USER_LOGIN_FAIL.getReturnMsg() + ":TOKEN错误");
		}
		PublicMethod.getResponse().setHeader(UserAuthMethod.LOGIN_TOKEN_NAME, token);
		PublicMethod.getResponse().setHeader("Access-Control-Expose-Headers", UserAuthMethod.LOGIN_TOKEN_NAME);
		redisService.set(UserAuthMethod.LOGIN_REDIS_DB, key, JSONTools.obj2json(user), UserAuthMethod.LOGIN_REDIS_TIME);
	}

	@Override
	@Transactional
	public UserResponse updateUser(UserUpdateRequest requestParam) throws ProAffairException {
		User user = BeanMapper.map(requestParam, User.class);
		User u = userMapper.selectByPrimaryKey(requestParam.getId());
		if (requestParam.getHeadImageUrl() != null && !requestParam.getHeadImageUrl().equals(u.getHeadImageUrl())) {
			qiniuUtils.delete(u.getHeadImageUrl());
		}
		int result = userMapper.updateByPrimaryKeySelective(user);
		if (result == 0) {
			throw new ProRunTimeException(ErrCode.USER_UPDATE_FAIL.getReturnCode(), ErrCode.USER_UPDATE_FAIL.getReturnMsg());
		}
		user = userMapper.selectByPrimaryKey(requestParam.getId());
		return BeanMapper.map(user, UserResponse.class);
	}

	@Override
	@Transactional
	public UserResponse bindPhoneNumber(Long userId, String phone, String code) throws ProAffairException {
		String redisKey = phone + "_" + PublicConstant.VERIFICATION_CODE_TYPE_1;
		Object obj = redisService.get(UserAuthMethod.LOGIN_REDIS_DB, redisKey);
		if (StringUtils.isEmpty(obj)) {
			throw new ProRunTimeException(ErrCode.VERIFICATIONCODE_BE_OVERDUE.getReturnCode(), ErrCode.VERIFICATIONCODE_BE_OVERDUE.getReturnMsg());
		}
		if (!obj.toString().equals(code)) {
			throw new ProRunTimeException(ErrCode.VERIFICATIONCODE_FAIL.getReturnCode(), ErrCode.VERIFICATIONCODE_FAIL.getReturnMsg());
		}
		User user = new User();
		user.setId(userId);
		user.setPhone(phone);
		int result = userMapper.updateByPrimaryKeySelective(user);
		if (result == 0) {
			throw new ProRunTimeException(ErrCode.USER_PHONE_BIND_FAIL.getReturnCode(), ErrCode.USER_PHONE_BIND_FAIL.getReturnMsg());
		}
		return BeanMapper.map(userMapper.selectByPrimaryKey(userId), UserResponse.class);
	}

	@Override
	@Transactional
	public void sendVerificationCode(SendVerificationCodeRequest request) throws ProAffairException {
		if (StringUtils.isEmpty(request.getPhone()) || !request.getPhone().matches(PublicConstant.REQEX_PHONE)) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		// 判断是否已经发送过验证码
		Date nowDate = new Date();
		List<SysSendVerificationCodeData> sendRecordList = sysSendVerificationCodeDataMapper.select(request.getType(), request.getPhone(), null,
				DateUtils.DF_SEPARATE_YMD.get().format(nowDate) + " 00:00:00", null);
		if (sendRecordList.size() >= 10) {
			throw new ProRunTimeException(ErrCode.ERR_VERIFICATIONCODE_FAIL.getReturnCode(), "验证码发送超过今日限制");
		}
		if (sendRecordList.size() > 0) {
			// 1分钟前
			Date dateBefore = DateUtils.getAddorSubtractTimes(nowDate, 0, -1, 0, 0, 0, 0);
			if (dateBefore.getTime() <= sendRecordList.get(sendRecordList.size() - 1).getCreateTime().getTime()) {
				throw new ProRunTimeException(ErrCode.ERR_VERIFICATIONCODE_FAIL.getReturnCode(), "验证码已经发送过");
			}
		}
		User user = null;
		if (request.getType() == PublicConstant.VERIFICATION_CODE_TYPE_1) {// 绑定手机号/更换手机号
			user = userMapper.selectByPhone(request.getPhone());
			if (user != null) {
				throw new ProRunTimeException(ErrCode.USER_PHONE_HAVE_EXISTED.getReturnCode(), ErrCode.USER_PHONE_HAVE_EXISTED.getReturnMsg());
			}
		}
		if (request.getType() == PublicConstant.VERIFICATION_CODE_TYPE_3) {// 修改密码
			user = userMapper.selectByPhone(request.getPhone());
			if (user == null) {
				throw new ProRunTimeException(ErrCode.USER_PHONE_NO_EXIST.getReturnCode(), ErrCode.USER_PHONE_NO_EXIST.getReturnMsg());
			}
		}
		String code = PublicMethod.getCode(6);// 生成验证码

		// 短信平台发送验证码
		String sendSmsParam = "{\"code\":\"" + code + "\"}";
		SendSms.sendSms(request.getPhone(), SendSms.HEAD, SendSms.TEMPLATE_CODE_VERIFICATION_CODE, sendSmsParam);

		String redisKey = request.getPhone() + "_" + request.getType();
		if (!redisService.set(UserAuthMethod.LOGIN_REDIS_DB, redisKey, code, PublicConstant.VERIFICATION_CODE_REDIS_TIME)) {
			throw new ProRunTimeException(ErrCode.ERR_VERIFICATIONCODE_FAIL.getReturnCode(), ErrCode.ERR_VERIFICATIONCODE_FAIL.getReturnMsg());
		}

		// 记录到数据库
		SysSendVerificationCodeData sysSendVerificationCodeData = new SysSendVerificationCodeData();
		sysSendVerificationCodeData.setType(request.getType());
		sysSendVerificationCodeData.setPhone(request.getPhone());
		sysSendVerificationCodeData.setSendIp(request.getIp());
		sysSendVerificationCodeData.setVerificationCode(code);
		sysSendVerificationCodeDataMapper.insertSelective(sysSendVerificationCodeData);
	}

	@Override
	@Transactional
	public UserResponse h5WechatLogin(H5WechatLoginRequest request) throws ProAffairException {
		if (StringUtils.isEmpty(request.getAppid()) || StringUtils.isEmpty(request.getCode())) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		User userPhone = null;
		if (!StringUtils.isEmpty(request.getPhone())) {
			userPhone = userMapper.selectByPhone(request.getPhone());
			if (userPhone == null) {
				throw new ProRunTimeException(ErrCode.USER_PHONE_NO_EXIST.getReturnCode(), ErrCode.USER_PHONE_NO_EXIST.getReturnMsg());
			}
		}
		// 获取openid
		WechatAccessToken token = tencentUtils.getH5OpenId(request.getAppid(), request.getCode());
		if (token == null || StringUtils.isEmpty(token.getOpenid())) {
			throw new ProRunTimeException(ErrCode.USER_GETWECHAT_FAIL.getReturnCode(), ErrCode.USER_GETWECHAT_FAIL.getReturnMsg());
		}
		User user = userMapper.selectByAppidAndOpenid(request.getAppid(), token.getOpenid());
		if (!StringUtils.isEmpty(request.getPhone())) {// 手机号绑定微信
			if (user != null) {
				throw new ProRunTimeException(ErrCode.USER_WECHAT_BIND_FAIL.getReturnCode(), "该微信信息已经登录过无法完成绑定");
			}
			if (request.getIsSilenceAuth() == 0) {
				if (StringUtils.isEmpty(userPhone.getHeadImageUrl())) {
					WechatUserInfo userInfo = tencentUtils.getWechatUserInfo(token.getAccess_token(), token.getOpenid());
					if (userInfo != null) {
						userPhone.setUserName(userInfo.getNickname());
						userPhone.setHeadImageUrl(userInfo.getHeadimgurl());
					}
				}
			}
			if (!StringUtils.isEmpty(token.getUnionid())) {
				userPhone.setUnionid(token.getUnionid());
			}
			UserLogin login = new UserLogin();
			login.setUserId(userPhone.getId());
			login.setAppid(request.getAppid());
			login.setOpenid(token.getOpenid());
			if (userLoginMapper.insertSelective(login) == 0) {
				throw new ProRunTimeException(ErrCode.USER_DB_INSERT_FAIL.getReturnCode(), ErrCode.USER_DB_INSERT_FAIL.getReturnMsg());
			}
			if (userMapper.updateByPrimaryKeySelective(userPhone) == 0) {
				throw new ProRunTimeException(ErrCode.USER_WECHAT_BIND_FAIL.getReturnCode(), ErrCode.USER_WECHAT_BIND_FAIL.getReturnMsg());
			} else {
				return BeanMapper.map(userPhone, UserResponse.class);
			}
		}
		if (user == null) {
			user = new User();
			user.setUserName("用户_" + token.getOpenid().substring(token.getOpenid().length() - 6));
			if (request.getRecommendUserId() != null && PublicMethod.isNumber(request.getRecommendUserId().toString())) {
				user.setRecommendUserId(request.getRecommendUserId());
			}
			if (!StringUtils.isEmpty(token.getUnionid())) {
				user.setUnionid(token.getUnionid());
			}
			int result = userMapper.insertSelective(user);
			if (result == 0) {
				throw new ProRunTimeException(ErrCode.USER_DB_INSERT_FAIL.getReturnCode(), ErrCode.USER_DB_INSERT_FAIL.getReturnMsg());
			}
			UserLogin login = new UserLogin();
			login.setUserId(user.getId());
			login.setAppid(request.getAppid());
			login.setOpenid(token.getOpenid());
			if (userLoginMapper.insertSelective(login) == 0) {
				throw new ProRunTimeException(ErrCode.USER_DB_INSERT_FAIL.getReturnCode(), ErrCode.USER_DB_INSERT_FAIL.getReturnMsg());
			}
			user = userMapper.selectByAppidAndOpenid(request.getAppid(), token.getOpenid());
		}
		if (user == null) {
			throw new ProRunTimeException(ErrCode.USER_LOGIN_FAIL.getReturnCode(), ErrCode.USER_LOGIN_FAIL.getReturnMsg() + ":无该用户信息");
		}
		if (request.getIsSilenceAuth() == 0) {
			if (StringUtils.isEmpty(user.getHeadImageUrl())) {
				WechatUserInfo userInfo = tencentUtils.getWechatUserInfo(token.getAccess_token(), token.getOpenid());
				if (userInfo != null) {
					user.setUserName(userInfo.getNickname());
					user.setHeadImageUrl(userInfo.getHeadimgurl());
					userMapper.updateByPrimaryKeySelective(user);
				}
			}
		}
		setLoginToken(user);
		return BeanMapper.map(user, UserResponse.class);
	}

	@Override
	public UserResponse userMessage(Long userId) throws ProException {
		User user = userMapper.selectByPrimaryKey(userId);
		if (user == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return BeanMapper.map(user, UserResponse.class);
	}
	
	
	@Override
	@Transactional
	public UserResponse h5BindPhone(H5BindPhoneRequest request) throws ProAffairException {
		String redisKey = request.getPhone() + "_" + PublicConstant.VERIFICATION_CODE_TYPE_6;
		Object objcode = redisService.get(UserAuthMethod.LOGIN_REDIS_DB, redisKey);
		if (StringUtils.isEmpty(objcode)) {
			throw new ProRunTimeException(ErrCode.VERIFICATIONCODE_BE_OVERDUE.getReturnCode(), ErrCode.VERIFICATIONCODE_BE_OVERDUE.getReturnMsg());
		}
		if (!objcode.toString().equals(request.getCode())) {
			throw new ProRunTimeException(ErrCode.VERIFICATIONCODE_FAIL.getReturnCode(), ErrCode.VERIFICATIONCODE_FAIL.getReturnMsg());
		}
		Object obj = redisService.get(UserAuthMethod.LOGIN_REDIS_DB, UserAuthMethod.getBindPhoneRedisKey(request.getAppid(), request.getOpenid()));
		if (StringUtils.isEmpty(obj)) {
			throw new ProRunTimeException(ErrCode.USER_LOGIN_BE_OVERDUE.getReturnCode(), ErrCode.USER_LOGIN_BE_OVERDUE.getReturnMsg() + ".");
		}
		String str[] = obj.toString().split(">>>");
		WechatAccessToken tencentLoginBean = JSONTools.jsonStr2obj(str[0], WechatAccessToken.class);
		if (tencentLoginBean == null) {
			throw new ProRunTimeException(ErrCode.USER_LOGIN_BE_OVERDUE.getReturnCode(), ErrCode.USER_LOGIN_BE_OVERDUE.getReturnMsg() + "..");
		}
		User user = userMapper.selectByAppidAndOpenid(request.getAppid(), request.getOpenid());
		if (user == null) {
			user = userMapper.selectByPhone(request.getPhone());
			if (user == null) {
				XcxLoginRequest requestParam = JSONTools.jsonStr2obj(str[0], XcxLoginRequest.class);
				user = new User();
				user.setUserName("用户_" + tencentLoginBean.getOpenid().substring(tencentLoginBean.getOpenid().length() - 6));
				// 绑定手机号
				user.setPhone(request.getPhone());
				if (requestParam.getRecommendUserId() != null && PublicMethod.isNumber(requestParam.getRecommendUserId().toString())) {
					user.setRecommendUserId(requestParam.getRecommendUserId());
				}
				if (!StringUtils.isEmpty(tencentLoginBean.getUnionid())) {
					user.setUnionid(tencentLoginBean.getUnionid());
				}
				int result = userMapper.insertSelective(user);
				if (result == 0 || user.getId() == null) {
					throw new ProRunTimeException(ErrCode.USER_DB_INSERT_FAIL.getReturnCode(), ErrCode.USER_DB_INSERT_FAIL.getReturnMsg());
				}
			} else {
				// 绑定手机号
				user.setPhone(request.getPhone());
			}
		} else {
			// 绑定手机号
			user.setPhone(request.getPhone());
		}
		UserLogin userLogin = userLoginMapper.selectByUserIdAndAppid(user.getId(), request.getAppid());
		if (userLogin == null) {
			UserLogin login = new UserLogin();
			login.setUserId(user.getId());
			login.setAppid(request.getAppid());
			login.setOpenid(tencentLoginBean.getOpenid());
			if (userLoginMapper.insertSelective(login) == 0) {
				throw new ProRunTimeException(ErrCode.USER_DB_INSERT_FAIL.getReturnCode(), ErrCode.USER_DB_INSERT_FAIL.getReturnMsg());
			}
		}

		user = userMapper.selectByAppidAndOpenid(request.getAppid(), tencentLoginBean.getOpenid());
		if (user == null) {
			throw new ProRunTimeException(ErrCode.USER_LOGIN_BE_OVERDUE.getReturnCode(), ErrCode.USER_LOGIN_BE_OVERDUE.getReturnMsg());
		}

		if (StringUtils.isEmpty(user.getHeadImageUrl())) {
			WechatUserInfo userInfo = tencentUtils.getWechatUserInfo(tencentLoginBean.getAccess_token(), tencentLoginBean.getOpenid());
			if (userInfo != null) {
				user.setUserName(userInfo.getNickname());
				user.setHeadImageUrl(userInfo.getHeadimgurl());
				userMapper.updateByPrimaryKeySelective(user);
			}
		}

		redisService.remove(UserAuthMethod.LOGIN_REDIS_DB, UserAuthMethod.getBindPhoneRedisKey(request.getAppid(), request.getOpenid()));

		setLoginToken(user);
		return BeanMapper.map(user, UserResponse.class);
	}
}
